/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worldscale.xp.wslib.im.jabber.impl;

import net.worldscale.xp.wslib.im.jabber.JabberClient;
import net.worldscale.xp.wslib.im.jabber.JabberID;
import net.worldscale.xp.wslib.im.jabber.JabberMessageList;
import net.worldscale.xp.wslib.im.jabber.JabberRosterModel;

/**
 *
 * @author Kun
 */
public final class ImplJabberClient implements JabberClient {

    private final ClientSession mClientSession;
    private boolean mIsInit = false;

    public static JabberClient newInstance() {
        ImplJabberClient jc = new ImplJabberClient();
        jc.init();
        return jc;
    }

    private ImplJabberClient() {
        mClientSession = new ClientSession();
    }

    private void init() {
        if (mIsInit) {
            return;
        } else {
            mIsInit = true;
        }
        (new Thread() {

            @Override
            public void run() {
                mClientSession.proc();
            }
        }).start();
    }

    public void close() {
        try {
            this.mClientSession.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Dispatcher getDispatcher() {
        try {
            return this.mClientSession.getCurUser().getCurWork().getDispatcher();
        } catch (Exception e) {
            return null;
        }
    }

    public int getPhase() {
        return this.mClientSession.getPhase();
    }

    public boolean login(LoginContext lc) {
        this.mClientSession.login(lc);
        return true;
    }

    public void logout() {
        try {
            this.mClientSession.getCurUser().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setRosterManager(RosterManager rm) {
        this.mClientSession.getEventRouter().setRosterManager(rm);
    }

    public void addPhaseListener(PhaseListener pl) {
        this.mClientSession.getEventRouter().addPhaseListener(pl);
    }

    public void addStreamListener(StreamListener sl) {
        this.mClientSession.getEventRouter().addStreamListener(sl);
    }

    public void removePhaseListener(PhaseListener pl) {
        this.mClientSession.getEventRouter().removePhaseListener(pl);
    }

    public void removeStreamListener(StreamListener sl) {
        this.mClientSession.getEventRouter().removeStreamListener(sl);
    }

    public JabberRosterModel getRosterModel() {
        final UserSession us = this.mClientSession.getCurUser();
        if (us == null) {
            return null;
        }
        return us.getRosterModel();
    }

    public JabberMessageList getMessageList() {
        final UserSession us = this.mClientSession.getCurUser();
        if (us == null) {
            return null;
        }
        return us.getMessageList();
    }

    public JabberID getLocalJID() {
        try {
            return this.mClientSession.getCurUser().getCurWork().getFullJabberID();
        } catch (Exception e) {
            return null;
        }
    }
}
